package cz.cuni.mff.ufal.volk;

/**
 * <p>Thrown to indicate that the language language is not supported by the requested operation.</p>
 * <p>The not supported language that caused the exception can be retrieved by the
 * {@link UnsupportedLanguageException#getLanguage()} method.</p>
 * 
 * @author Bartłomiej Etenkowski
 */
public class UnsupportedLanguageException extends UnsupportedOperationException {  
  private static final long serialVersionUID = 9040343324687606847L;

  /**
   * Constructs an UnsupportedLanguageException with no detail message.
   * 
   * @param language the language which caused the exception
   */
  public UnsupportedLanguageException(String language) {
    setLanguage(language);
  }
  
  /**
   * Constructs an UnsupportedLanguageException with the specified detail message.
   *  
   * @param  language the language which caused the exception
   * @param  message the detail message (which is saved for later retrieval
   *         by the {@link Throwable#getMessage()} method).
   */
  public UnsupportedLanguageException(String language, String message) {
    super(message);
    setLanguage(language);
  }
  
  /**
   * Constructs an UnsupportedLanguageException with the specified defail message 
   * and cause.
   * 
   * @param  language the language which caused the exception
   * @param  message the detail message (which is saved for later retrieval
   *         by the {@link Throwable#getMessage()} method).
   * @param  cause the cause (which is saved for later retrieval by the
   *         {@link Throwable#getCause()} method).  (A <tt>null</tt> value
   *         is permitted, and indicates that the cause is nonexistent or
   *         unknown.)
   */
  public UnsupportedLanguageException(String language, String message, Throwable cause) {
    super(message, cause);
    setLanguage(language);
  }
  
  /**
   * Constructs an UnsupportedLanguageException with the specified cause.
   * 
   * @param  language the language which caused the exception
   * @param  cause the cause (which is saved for later retrieval by the
   *         {@link Throwable#getCause()} method).  (A <tt>null</tt> value
   *         is permitted, and indicates that the cause is nonexistent or
   *         unknown.)
   */
  public UnsupportedLanguageException(String language, Throwable cause) {
    super(cause);
    setLanguage(language);
  }
  
  private final static int MAX_LENGTH = 20;
  private String language;

  private void setLanguage(String language) {
    if (language == null || language.length() <= MAX_LENGTH) {
      this.language = language;
    } else {
      this.language = language.substring(0, MAX_LENGTH);
    }
  }
  
  /**
   * Returns the language which caused the exception.
   * 
   * @return the language which caused the exception
   */
  public final String getLanguage() {
    return language == null ? "" : language;
  }
}
